﻿﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using GMap.NET.WindowsPresentation;
using System;
using System.Windows.Media.Imaging;


namespace ElStationUI
{
  
        public partial class StationMarker
        {
            private Popup Popup;
            private Label Label;
            public GMapMarker Marker;
            private MainWindow MainWindow;
            private String imageSrcUri;
            private BitmapImage iconBlue;
            private BitmapImage iconRed;
            public bool IsDraggable { get; set; }
            private bool isSelected;

            public bool IsSelected
            {
                get { return isSelected; }
                set { isSelected = value; icon.Source = value ? iconRed :  iconBlue; }  //yea, I know, probably shouldn't be done that way, but it is fast
            }
            public int StationId { get; set; }
            private string title;

            public string Title
            {
                get { return title; }
                set { title = value; Label.Content = title; }   //same problem here but at least it is making sure it is consistent at all points
            }
            //IconImage img;
            // enum IconImage { iconRed = 1, iconBlue = 2 };


            public StationMarker(MainWindow window, GMapMarker marker, string title) //, IconImage iconImg
            {
                this.InitializeComponent();
                this.title = title;
                this.MainWindow = window;
                this.Marker = marker;
              //  this.iconImg = iconImg;
               // UpdateIconImage(iconImg);
                iconBlue = new BitmapImage(new Uri(@"Resources\blue-dot.png", UriKind.Relative));
                iconRed = new BitmapImage(new Uri(@"Resources\red-dot.png", UriKind.Relative));

                Popup = new Popup();
                Label = new Label();


                this.Loaded += new RoutedEventHandler(CustomMarkerDemo_Loaded);
                this.SizeChanged += new SizeChangedEventHandler(CustomMarkerDemo_SizeChanged);
                this.MouseEnter += new MouseEventHandler(MarkerControl_MouseEnter);
                this.MouseLeave += new MouseEventHandler(MarkerControl_MouseLeave);
                this.MouseMove += new MouseEventHandler(CustomMarkerDemo_MouseMove);
                this.MouseLeftButtonUp += new MouseButtonEventHandler(CustomMarkerDemo_MouseLeftButtonUp);
                this.MouseLeftButtonDown += new MouseButtonEventHandler(CustomMarkerDemo_MouseLeftButtonDown);
                

                Popup.Placement = PlacementMode.Mouse;
                {
                    Label.Background = null;
                    Label.Foreground = Brushes.White;
                    Label.BorderBrush = null;
                    Label.BorderThickness = new Thickness(2);
                    Label.Padding = new Thickness(5);
                    Label.FontSize = 22;
                    Label.Content = title;
                }
                Popup.Child = Label;
            }

            void CustomMarkerDemo_Loaded(object sender, RoutedEventArgs e)
            {
                if (icon.Source.CanFreeze)
                {
                    icon.Source.Freeze();
                }
            }

            void CustomMarkerDemo_SizeChanged(object sender, SizeChangedEventArgs e)
            {
                Marker.Offset = new Point(-e.NewSize.Width / 2, -e.NewSize.Height);
            }

            void CustomMarkerDemo_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed && IsMouseCaptured)
                {
                    Point p = e.GetPosition(MainWindow.mapView);
                    Marker.Position = MainWindow.mapView.FromLocalToLatLng((int)p.X, (int)p.Y);
                }
            }

            void CustomMarkerDemo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                if (!IsMouseCaptured && IsDraggable)
                {
                    Mouse.Capture(this);
                }
            }

            void CustomMarkerDemo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                if (IsMouseCaptured)
                {
                    Mouse.Capture(null);
                }
            }

            void MarkerControl_MouseLeave(object sender, MouseEventArgs e)
            {
                Marker.ZIndex -= 10000;
                Popup.IsOpen = false;
            }

            void MarkerControl_MouseEnter(object sender, MouseEventArgs e)
            {
                Marker.ZIndex += 10000;
                Popup.IsOpen = true;
            }

            

   
        }
    }

